package com.wang.spark
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
object ScalaWordCount {
  /**
    *  Created by wang on 2018/12/26
    * @param args
    */
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("ScalaWordCount")
    val sc = new SparkContext(conf)
    val lines : RDD[String] = sc.textFile(args(0))
   val words : RDD[String] = lines.flatMap(_.split(" "))
    val wordAndOne : RDD[(String,Int)] = words.map((_,1))
    val reduced : RDD[(String,Int)] = wordAndOne.reduceByKey(_+_)
    val sorted :RDD[(String,Int)] = reduced.sortBy(_._2,false)
    sorted.saveAsTextFile(args(1))
    sc.stop()
  }

}
